package com.example.myapplication.base.mvvm.ui;

import com.example.myapplication.base.mvvm.BaseViewModel;
import com.example.myapplication.base.mvp.IBaseUiView;
import com.example.myapplication.utils.Toast;
import com.harmonyos.livedata.Observer;
import com.harmonyos.viewmodel.ViewModelProvider;
import com.harmonyos.viewmodel.ViewModelStore;
import com.harmonyos.viewmodel.ViewModelStoreOwner;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.impl.LoadingPopupView;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

public abstract class BaseViewModelAbilitySlice<VM extends BaseViewModel> extends AbilitySlice implements IBaseUiView, ViewModelStoreOwner {

    public abstract int getUIContent();

    public abstract void initComponent();
    public void initClickListener(){}
    protected VM mViewModel;

    protected abstract Class<VM> getViewModelClass();

    public Context mContext;

    public Intent intent;

    private ViewModelStore viewModelStore;

    protected EventHandler eventHandler;

    private Runnable cancleLoadingTast;

    private LoadingPopupView loadingPopup;

    public void getData(){}

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        this.intent = intent;
        loadingPopup = new XPopup.Builder(this).asLoading();
        eventHandler = new EventHandler(EventRunner.getMainEventRunner());
        cancleLoadingTast = new Runnable() {
            @Override
            public void run() {
                if (loadingPopup != null)
                    loadingPopup.dismiss();
            }
        };
        mContext = this;
        mViewModel = new ViewModelProvider(getViewModelStore(), ViewModelProvider.HarmonyViewModelFactory.getInstance(getApplicationContext())).get(getViewModelClass());

        initUiChangeEvent();
        initViewObservable();
        beforsetUIContent();
        super.setUIContent(getUIContent());
        this.initComponent();
        this.initClickListener();
        getData();
    }

    public void initViewObservable() {

    }

    private void initUiChangeEvent() {
        mViewModel.getUIChangeLiveDataEvent().getShowLoadingNowEvent()
                .observe(this, new Observer<Boolean>() {
                    @Override
                    public void onChanged(Boolean aBoolean) {
                        if (aBoolean) {
                            showLoadingDialogNow(true);
                        } else {
                            showLoadingDialog();
                        }
                    }
                });
        mViewModel.getUIChangeLiveDataEvent().getDismissLoadingDialogEvent()
                .observe(this, new Observer<Boolean>() {
                    @Override
                    public void onChanged(Boolean aBoolean) {
                        dismissLoadingDialog();
                    }
                });
        mViewModel.getUIChangeLiveDataEvent().getShowToastMessageEvent()
                .observe(this, new Observer<String>() {
                    @Override
                    public void onChanged(String s) {
                        showToast(s);
                    }
                });
        mViewModel.getUIChangeLiveDataEvent().getShowEmptyViewEvent()
                .observe(this, new Observer<Boolean>() {
                    @Override
                    public void onChanged(Boolean aBoolean) {
                        showEmptyView();
                    }
                });
        mViewModel.getUIChangeLiveDataEvent().getShowNetworkErrorEvent()
                .observe(this, new Observer<Boolean>() {
                    @Override
                    public void onChanged(Boolean aBoolean) {
                        showErrorView();
                    }
                });

    }

    @Override
    public ViewModelStore getViewModelStore() {
        if (viewModelStore == null) {
            viewModelStore = new ViewModelStore();
        }
        return viewModelStore;

    }

    @Override
    protected void onInactive() {
        super.onInactive();

    }

    @Override
    protected void onStop() {
        getViewModelStore().clear();
        super.onStop();
    }


    public String getString(int resId) {
        try {
            return getResourceManager().getElement(resId).getString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public int getColor(int colorId) {
        try {
            return getResourceManager().getElement(colorId).getColor();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    protected void onBackPressed() {
        super.onBackPressed();
        dismissLoadingDialog();
    }

    @Override
    public void dismissLoadingDialog() {
        if (loadingPopup != null) {
            loadingPopup.dismiss();
            eventHandler.removeTask(cancleLoadingTast);
        }
    }

    public FractionManager getFractionManager() {
        Ability ability = getAbility();
        if (ability instanceof FractionAbility) {
            FractionAbility fractionAbility = (FractionAbility) ability;
            return fractionAbility.getFractionManager();
        }
        return null;
    }


    public void beforsetUIContent() {

    }

    @Override
    public void showLoadingDialog() {
        if (loadingPopup != null && !loadingPopup.isShow()) {
            loadingPopup.show();
        }
    }

    @Override
    public void showEmptyView() {

    }

    @Override
    public void showErrorMsg(int errorCode, String msg) {
        showToast(msg);
    }

    @Override
    public void showToast(String message) {
        Toast.makeToast(mContext, message, 3000).show();
    }

    @Override
    public void showLoadingDialogNow(boolean isNow) {
        if (isNow) {
            showLoadingDialog();
        } else {
            if (loadingPopup != null && !loadingPopup.isShow()) {
                postTask(new Runnable() {
                    @Override
                    public void run() {
                        showLoadingDialog();
                    }
                }, 750);
            }
        }
        if (eventHandler != null) {
            eventHandler.postTask(cancleLoadingTast, 15000);
        }
    }

    @Override
    public void showErrorView() {

    }

    @Override
    public void showNoMoreView() {

    }

}
